import { _decorator, Collider2D, Component, Node } from 'cc';
import { Bullet_2 } from './Bullet_2';
import { Unit } from './Unit';
import { DestructableCom } from './Component/DestructableCom';
import { UnitType } from '../Util/Global';
import { UnitPlanetBase } from './UnitPlanetBase';
const { ccclass, property } = _decorator;

/**
 * 圆形范围子弹
 */
@ccclass('Bullet_3')
export class Bullet_3 extends Bullet_2 {
    /**伤害类型：1单次伤害，2持续伤害 */
    public damageType: number = 1;
    public maxRadius: number = 0;
    /**触发间隔 */
    public triggerInterval: number = 0.1;
    /**处理对象 [操作者，处理次数]*/
    protected triggerMarkMap: Map<Unit, Map<Component, number>> = new Map();

    /**
     * 标记处理
     * @param target 处理对象
     * @param from 操作来源
     */
    public markTrigger(target: Unit, from: Component) {
        let mark = this.triggerMarkMap.get(target) || new Map();
        let count = mark.get(from) || 0;
        mark.set(from, ++count);
        this.triggerMarkMap.set(target, mark);
    }

    /**
     * 获取处理次数
     * @param target 处理对象
     * @param from 操作来源
     * @return mark次数
     */
    public getMarkCount(target: Unit, from: Component): number {
        if (from == null) { return 0; }
        let mark = this.triggerMarkMap.get(target) || new Map();
        return mark.get(from) || 0;
    }

    protected onDisable(): void {
        super.onDisable();
        this.triggerMarkMap.clear();
        this.trigger.radius = 10;
        this.canLifeTime = 3;
    }

    private _triggerTick: number = 0;
    updateLogic(dt: number) {
        if (this._lifeTime >= this.canLifeTime) {

            this.DestroySelf();
        }
        else if (this._lifeTime > 0 && this.trigger.radius < this.maxRadius) {
            this.alertFieldRadius = this._lifeTime * this.maxRadius / this.canLifeTime;
            this.trigger.radius = this.alertFieldRadius;

            if (this._triggerTick <= 0) {
                this.trigger.apply();
                this._triggerTick = this.triggerInterval;
            }
            else {
                this._triggerTick -= dt;
            }

            this.triggerMarkMap.forEach((v, k) => {
                if (!k.death && k.unitType > UnitType.UnitSpacecra) {
                    k.maxSpeed_currentFame = 50;
                }
            })
        }
    }

    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        // 只在两个碰撞体开始接触时被调用一次
        if (!this.isValid) {
            return;
        }

        let other = otherCollider.node.getComponent(Unit);
        let destructable = other.getComponent(DestructableCom);
        if (this.IsEnemy(other)) {
            if (destructable) {
                if ((this.damageType == 1 && this.getMarkCount(other, destructable) == 0)
                    || this.damageType == 2) {
                    destructable.OnDamage(this);
                    this.markTrigger(other, destructable);
                }
            }

            if (other.unitType > UnitType.UnitSpacecra) {
                let planet = <UnitPlanetBase>other;
                let dir = this.GetDirToOther(planet);
                planet.forceTrackTarget = null;
                planet.MoveTo_byDir(dir, true, 50);//给个反向速度
            }
            // this.DestroySelf();
        }
        else if (other == this.target) {
            console.log('警告==>击中目标！但是是友方？！！');
        }
    }

    IsEnemy(unit: Unit): boolean {
        if (unit.unitType >= UnitType.UnitSun) {
            return false;
        }
        if (unit &&
            (!this.parentUnit || unit != this.parentUnit)) {
            return true;
        }
        return false;
    }
}


